<?php

namespace Worker\Controller;

use Worker\Common\Library\Page;
use Worker\Logic;

class RecommController extends WorkerBaseController
{
    private $recommLogic;
    public function _initialize()
    {
        parent::_initialize();
        $this->recommLogic = new Logic\RecommLogic();
    }

    /**
     * 我的徒弟
     *
     * @return void
     */
    public function userList()
    {
        $this->display();
    }

    /**
     * 获取徒弟列表
     *
     * @return void
     */
    public function doGetUserList()
    {
        $page = I('post.page');
        $type = I('post.type');

        $userInfo = $this->getUserInfo();
        $userId = $userInfo["id"];
        
        $page = $page?$page:1;
        $pageSize = 20;

        $maxLevel = $type == 1?6:1;

        $listRes = $this->recommLogic->getRecommUserList($userId, $maxLevel);
        if (static::$res->isErr($listRes)) {
            $this->ajaxReturn($listRes);
        }
        $userList = $listRes['Result'];
        $total = count($userList);

        if (empty($userList)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $start = ($page - 1) * $pageSize;
        $list = array();
        for ($i=$start; $i < $start+$pageSize; $i++) {
            if (!empty($userList[$i])) {
                $list[] = $userList[$i];
            }
        }

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetUserList');

        $this->ajaxReturn(static::$res->suc("成功", $html));
    }

    /**
     * 获取任务列表
     *
     * @return void
     */
    public function taskList()
    {
        $this->display();
    }

    /**
     * 获取徒弟任务列表
     *
     * @return void
     */
    public function doGetTaskList()
    {
        $finishTime = I('post.finish_time');
        $page = I('post.page');
        $page = $page?$page:1;
        $pageSize = 20;

        if (empty($finishTime)) {
            $this->ajaxReturn(static::$res->err("参数异常"));
        }

        
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $taskListRes = $this->recommLogic->getRecommTaskList($userId, $finishTime, $page, $pageSize);
        if (static::$res->isErr($taskListRes)) {
            $this->ajaxReturn($taskListRes);
        }

        $data = $taskListRes["Result"];
        $list = $data["list"];

        $total = $data["count"];
        $incomes = $data["incomes"];
        if (empty($total)) {
            $this->ajaxReturn(static::$res->err("徒弟未进行任何任务"));
        }
        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->recommLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("total", $total);
        $this->assign("incomes", $incomes);
        $html = $this->fetch('doGetTaskList');

        $this->ajaxReturn(static::$res->suc("成功", $html));
    }
}
